﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TetrisService
{
    /// <summary>
    /// This class represents a registered user on the application.
    /// </summary>
    public class User
    {
        private string userName;
        private string password;
        private int[] scores;

        /// <summary>
        /// Initializes a new instance of the <see cref="User"/> class.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="scores">The scores.</param>
        public User(string userName, string password, int[] scores)
        {
            this.userName = userName;
            this.password = password;
            this.scores = scores;
        }

        /// <summary>
        /// Gets the name of the user.
        /// </summary>
        /// <returns></returns>
        public string getUserName()
        {
            return userName;
        }

        /// <summary>
        /// Gets the scores.
        /// </summary>
        /// <returns></returns>
        public int[] getScores()
        {
            return scores;
        }

        /// <summary>
        /// Gets the valid scores.
        /// </summary>
        /// <returns></returns>
        public int[] getValidScores()
        {
            int[] validScores =  new int[scores.Length - 1];
            for (int i = 0; i < validScores.Length; i++)
            {
                validScores[i] = scores[i];
            }
            return validScores;
        }

        /// <summary>
        /// Adds the score to the ranking.
        /// </summary>
        /// <param name="newScore">The new score.</param>
        public void addScoreToFiveBest(int newScore)
        {
            Array.Sort<int>(scores);
            Array.Reverse(scores);
            scores[5] = newScore;
            Array.Sort<int>(scores);
            Array.Reverse(scores);
        }

        /// <summary>
        /// Checks if the provided password is valid.
        /// </summary>
        /// <param name="password">The provided password.</param>
        /// <returns></returns>
        public bool checkPassword(string password)
        {
            return this.password == password;
        }

        /// <summary>
        /// Gets the user password.
        /// </summary>
        /// <returns></returns>
        public string getPassword()
        {
            return password;
        }
    }
}